const path = require("path");

const {
    fileUploadError,
    unSupportedFileType,
    publishGoodsError,
    invalidGoodsID,
} = require("../constant/err.type");

const {
    createGoods,
    updateGoods,
    removeGoods,
    restoreGoods,
    findGoods,
} = require("../service/goods.service");

class GoodsController {
    async upload(ctx, next) {
        const { file } = ctx.request.files;
        const fileTypes = ["image/jpeg", "image/png"];
        if (file) {
            if (fileTypes.includes(file.type)) {
                return ctx.app.emit("error", unSupportedFileType, ctx);
            }
            ctx.body = {
                code: 0,
                message: "商品图片上传成功",
                result: {
                    goods_img: path.basename(file.filepath),
                },
            };
        } else {
            return ctx.app.emit("error", fileUploadError, ctx);
        }
    }

    async creat(ctx) {
        try {
            const { createdAt, updatedAt, ...res } = await createGoods(
                ctx.request.body
            );

            ctx.body = {
                code: 0,
                message: "发布商品成功",
                result: res,
            };
        } catch (error) {
            return ctx.app.emit("error", publishGoodsError, ctx);
        }
    }

    async update(ctx) {
        try {
            const res = await updateGoods(ctx.params.id, ctx.request.body);
            if (res) {
                ctx.body = {
                    code: 0,
                    message: "修改商品成功",
                    result: "",
                };
            } else {
                return ctx.app.emit("error", invalidGoodsID, ctx);
            }
        } catch (error) {
            console.error(error);
        }
    }

    async remove(ctx) {
        try {
            const res = await removeGoods(ctx.params.id);
            if (res) {
                ctx.body = {
                    code: 0,
                    message: "下架商品成功",
                    result: "",
                };
            } else {
                return ctx.app.emit("error", invalidGoodsID, ctx);
            }
        } catch (error) {
            console.error(error);
        }
    }

    async restore(ctx) {
        try {
            const res = await restoreGoods(ctx.params.id);
            if (res) {
                ctx.body = {
                    code: 0,
                    message: "上架商品成功",
                    result: "",
                };
            } else {
                return ctx.app.emit("error", invalidGoodsID, ctx);
            }
        } catch (error) {
            console.error(error);
        }
    }

    async findAll(ctx) {
        const { pageNum = 1, pageSize = 10 } = ctx.request.query;//从查询字符串中获取
        const { goods_name} = ctx.params;
        const res = await findGoods(pageNum, pageSize, goods_name);
        ctx.body = {
            code: 0,
            message: "获取商品列表成功",
            result: res
        };
    }

}

module.exports = new GoodsController();
